<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form
      :model="pwdForm"
      :rules="pwdFormRules"
      ref="ruleForm"
      label-width="100px"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="Changethepassword" type="primary"
          >修改密码</el-button
        >
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: 'UserPwd',
  data () {
    // 原来密码和现在密码不能一样
    const validatePass = (rule, value, callback) => {
      if (this.pwdForm.old_pwd === value) {
        callback(new Error('新密码和旧的密码不能一样'))
      } else {
        callback()
      }
    }
    // 确认密码表单校验
    const validatePass2 = (rule, value, callback) => {
      if (this.pwdForm.new_pwd !== value) {
        callback(new Error('两次输入密码不一致'))
      } else {
        callback()
      }
    }
    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      // 表单的验证规则对象
      pwdFormRules: {
        old_pwd: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          {
            pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/,
            message: '原密码为6-15为字母和数字组成',
            trigger: 'blur'
          }
        ],
        new_pwd: [
          { required: true, message: '请输入修改的密码', trigger: 'blur' },
          {
            pattern: /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,15}$/,
            message: '密码为6-15为字母和数字组成',
            trigger: 'blur'
          },
          { validator: validatePass }
        ],
        re_pwd: [
          { required: true, message: '请确认修改的密码', trigger: 'blur' },
          { validator: validatePass2 }
        ]
      }
    }
  },
  methods: {
    // 立即修改密码
    Changethepassword () {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //  修改密码
          this.$http
            .patch('/my/updatepwd', this.pwdForm)
            .then(({ data: res }) => {
              // console.log(res)
              // 如果修改成功

              if (res.code === 0) {
                // 提示用户
                this.$message({
                  message: res.message,
                  type: 'success'
                })
                // 清空表单
                this.resetForm()
                // 失败
              } else {
                this.$message.error(res.message)
              }
            })
        } else {
          this.$message.error('输入格式不正确')
          return false
        }
      })
    },
    // 重置
    resetForm (formName) {
      // console.log(this.$refs.ruleForm)
      this.$refs.ruleForm.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
